package cn.netuo.bumbersoll;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.rooten.frame.AppActivity;
import com.tencent.bugly.Bugly;

import java.util.UUID;

import cn.netuo.bumbersoll.entity.VideoAd;
import cn.netuo.bumbersoll.util.BsUtil;
import lib.grasp.helper.LinearLayoutHelper;
import lib.grasp.util.L;
import lib.grasp.util.TOAST;
import lib.grasp.widget.MessageBoxGrasp;

/**
 * 开发调试界面
 */
public class DevelopActivity extends AppActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private View mView;
    private LinearLayout mContainer;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //强制竖屏
        super.initView(savedInstanceState);
        mView = LayoutInflater.from(this).inflate(R.layout.activity_set, null);
        installView(mView, 1);
        init();
    }

    @Override
    protected void initActionMenu(MenuItem item) {
        super.initActionMenu(item);
        item.setVisible(false);
        setTitle("开发调试界面");
    }

    /** 初始化布局 */
    private void initView(){
        mContainer = mView.findViewById(R.id.ll_container);

        LinearLayoutHelper.addTransportSpace(this, mContainer, 20);
        LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "模拟心跳", "", "", "03", true);
        LinearLayoutHelper.addDivider(this, mContainer);
        LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "电量测试", "", "", "031", true);

        LinearLayoutHelper.addTransportSpace(this, mContainer, 20);
        LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "获取音量", "", "", "331", true);
        LinearLayoutHelper.addDivider(this, mContainer);
        LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "静音", "", "", "332", true);

        LinearLayoutHelper.addTransportSpace(this, mContainer, 20);
        LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "添加下载任务", "", "", "13", true);
        LinearLayoutHelper.addDivider(this, mContainer);
        LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "更新下载任务", "", "", "23", true);
        LinearLayoutHelper.addDivider(this, mContainer);
        LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "触发下载", "", "", "14", true);

        boolean isDevelopDev = BsUtil.getBuglyIsDevelopDev(this);
        LinearLayoutHelper.addTransportSpace(this, mContainer, 20);
        LinearLayoutHelper.addSwitcheInfoLine(this, this, mContainer, "Bugly开发设备",  isDevelopDev, "112");

        boolean enableLog = BsUtil.getBuglyEnableLog(this);
        LinearLayoutHelper.addDivider(this, mContainer);
        LinearLayoutHelper.addSwitcheInfoLine(this, this, mContainer, "Bugly允许Log调试",  enableLog, "113");
    }

    @Override
    public void onClick(View v) {
        switch ((String)v.getTag()){
            case "03": {
                App.getApp().getLocalBroadMgr().broadAction(BizConstant.BROAD_BIZ_HEARTBEAT);
                break;
            }

            case "031": {
                int ba = App.getApp().getBatteryReceiver().getBatteryLeftRate(this);
                TOAST.showShort(this, "剩余电量:" + String.valueOf(ba));
                break;
            }
            case "13": {
                L.logOnly("添加下载任务");
                long time = System.currentTimeMillis()/1000;

                VideoAd videoAd1 = new VideoAd();
                videoAd1.name = "lol.mp4";
                videoAd1.url = "https://sun-shade-1258997397.cos.ap-shanghai.myqcloud.com/lol.mp4";
                videoAd1.md5 = UUID.randomUUID().toString();
                videoAd1.indbtime = time;
                App.getApp().getTableMgr().getTbVideoAd().importArg(videoAd1);

                L.logOnly("添加下载任务");
                VideoAd videoAd = new VideoAd();
                videoAd.name = "沧海一声笑.mp4";
                videoAd.url = "https://sun-shade-1258997397.cos.ap-shanghai.myqcloud.com/%E6%B2%A7%E6%B5%B7%E4%B8%80%E5%A3%B0%E7%AC%91.mp4";
                videoAd.md5 = UUID.randomUUID().toString();
                videoAd.indbtime = time;
                App.getApp().getTableMgr().getTbVideoAd().importArg(videoAd);
                break;
            }

            case "23": {
                L.logOnly("更新下载任务");
                long time = System.currentTimeMillis()/1000;

                VideoAd videoAd1 = new VideoAd();
                videoAd1.name = "lol.mp4";
//                videoAd.url = "https://sun-shade-1258997397.cos.ap-shanghai.myqcloud.com/lol.mp4?q-sign-algorithm=sha1&q-ak=AKIDcMDvZvRbdBAGPxLOtbhyzZpRhzC198E5&q-sign-time=1555053214;1555055014&q-key-time=1555053214;1555055014&q-header-list=&q-url-param-list=&q-signature=82b0fc979e8462bc9df92861e5e23e82b6dfb7da&x-cos-security-token=a5ba895a883a2e58cd36f519c1b3114398939ebd10001";
                videoAd1.url = "https://sun-shade-1258997397.cos.ap-shanghai.myqcloud.com/lol.mp4";
                videoAd1.md5 = UUID.randomUUID().toString();
                videoAd1.indbtime = time;
                App.getApp().getTableMgr().getTbVideoAd().importArg(videoAd1);
                break;
            }
            case "14": {
                L.logOnly("触发下载");
                App.getApp().getLocalBroadMgr().broadAction(BizConstant.BROAD_VIDEO_TASK);
                break;
            }
            case "331":{
                int vol = BsUtil.getMediaVol(this);
                TOAST.showShort(this, "当前媒体音量:" + vol);
                break;
            }
            case "332":{
                BsUtil.setMediaVol(this, 0);
                TOAST.showShort(this, "操作成功");
                break;
            }
        }
    }

    private void init(){
        initView();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!buttonView.isPressed()) return;
        Object o = buttonView.getTag();
        if(!(o instanceof String)) return;
        switch ((String) o){
            case "112":{
                App.getApp().getTableMgr().getTbArg().importArg(BizConstant.ARG_BUGLY_IS_DEVELOP_DEV, isChecked ? "1" : "0");
                Bugly.setIsDevelopmentDevice(App.getApp(), isChecked);  // Bugly是否是开发设备
                L.logOnly("Bugly是否是开发设备:" + isChecked);
                break;
            }
            case "113":{
                App.getApp().getTableMgr().getTbArg().importArg(BizConstant.ARG_BUGLY_ENABLE_LOG, isChecked ? "1" : "0");
                MessageBoxGrasp.infoMsg(this, "提示", "修改Log调试需要重新启动APP, 现在重启?", false, v -> App.getApp().destroyApp(true));
                L.logOnly("Bugly允许Log调试:" + isChecked);
                break;
            }
            default: break;
        }
    }
}
